package m.system.util;

import m.system.RuntimeData;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;


public class FileUtil {
	/**
	 * 判断路径是否存在
	 */
	public static boolean existsPublicFile(String url_path){
		File file=new File(RuntimeData.getPublicWeb()+url_path);
		return file.exists();
	}
	public static boolean existsWebFile(String url_path){
		File file=new File(RuntimeData.getClassWeb()+url_path);
		return file.exists();
	}
	public static boolean existsRootFile(String url_path){
		File file=new File(RuntimeData.getPublicPath()+url_path);
		return file.exists();
	}
	public static boolean existsClassFile(String url_path){
		File file=new File(RuntimeData.getClassPath()+url_path);
		return file.exists();
	}
	public static File getRootFile(String url_path) throws IOException{
		return getFile(RuntimeData.getPublicPath()+url_path);
	}
	public static File getClassFile(String url_path) throws IOException{
		return getFile(RuntimeData.getClassPath()+url_path);
	}

	/**
	 * 获取公共目录相对路径文件
	 */
	public static File getPublicFile(String url_path) throws IOException{
		return getFile(RuntimeData.getPublicWeb()+url_path);
	}

	/**
	 * 写入公共目录相对路径文件
	 */
	public static void writePublicFile(String url_path,InputStream inStream) throws Exception{
		writeFile(RuntimeData.getPublicWeb()+url_path,inStream);
	}
	/**
	 * 写入公共目录相对路径文件
	 */
	public static void writePublicFile(String url_path,File file) throws Exception{
		writeFile(RuntimeData.getPublicWeb()+url_path,file);
	}
	/**
	 * 删除公共目录相对路径文件
	 */
	public static boolean deletePublicFile(String url_path) {
		File file=new File(RuntimeData.getPublicWeb()+url_path);
		if(file.exists()&&file.isFile()){
			return file.delete();
		}else{
			return false;
		}
	}
	public static File getDirs(String path){
		File file=new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		return file;
	}
	public static File getFile(String path) throws IOException{
		File file=new File(path);
		if(!file.exists()){
			getDirs(file.getPath().substring(0, file.getPath().lastIndexOf(file.getName())));
			file.createNewFile();
		}
		return file;
	}
	public static void writeFile(String filePath,File file) throws Exception {
		writeFile(filePath, Files.newInputStream(file.toPath()));
	}
	public static void writeFile(String filePath,InputStream inStream) throws Exception{
		Exception e1=null;
		OutputStream outSteam=null;
		try{
			outSteam = Files.newOutputStream(getFile(filePath).toPath());
			byte[] buffer = new byte[1024];
			int len;
			while ((len = inStream.read(buffer)) != -1) {
				outSteam.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e1 = e;
		} finally {
			if (null != inStream)
				inStream.close();
			if(null!=outSteam)
				outSteam.close();
		}
		if(null!=e1) throw e1;
	}
	public static void writeFile(String filePath,byte[] buffer) throws Exception{
		OutputStream outSteam=null;
		try {
			outSteam = Files.newOutputStream(getFile(filePath).toPath());
			outSteam.write(buffer, 0, buffer.length);
		}finally {
			if(null!=outSteam)
				outSteam.close();
		}
	}
}
